package cn.edu.hitwh.resume.web.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.hitwh.resume.dal.dao.CompanyDao;
import cn.edu.hitwh.resume.dal.entity.CompanyDo;
import cn.edu.hitwh.resume.web.controller.vo.ResultVo;

@Controller
public class CompanyController {

	@Autowired
	CompanyDao companyDao;
	
	@RequestMapping(value = {"/company", "/company/", "/company/index"})
	public String index(HttpSession session, ModelMap model) {
		Object user = session.getAttribute("company");
		if (null == user) {
			return "redirect:/company/login";
		} else {
			model.put("user", user);
		model.put("user", user);
			return "page/company/index";
		}
	}
	
	@RequestMapping(value = "/company/login", method = RequestMethod.GET)
	public String loginPage() {
		return "page/company/login";
	}
	
	@RequestMapping(value = "/company/login", method = RequestMethod.POST)
	public String loginAction(@RequestParam(value = "name") String name,
							   @RequestParam(value = "pwd") String pwd,
							   HttpSession session,
							   ModelMap model) {
		CompanyDo company = this.companyDao.retriveByName(name);
		model.put("current", "login");
		if (null == company) {
			model.put("error", "Name does not exist");
			return "page/company/login";
		} else {
			if (company.getPwd().equals(pwd)) {
				model.clear();
				session.setAttribute("company", company);
				return "redirect:/company/index";
			} else {
				model.put("error", "Password is incorrect");
				return "page/company/login";
			}
		}
	}
	
	@RequestMapping(value = "/company/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/company/login";
	}
	
	@RequestMapping(value = "/company/register", method = RequestMethod.POST)
	public String register(@RequestParam(value = "name") String name,
							 @RequestParam(value = "pwd") String pwd,
							 @RequestParam(value = "address") String address,
							 @RequestParam(value = "phone") String phone,
							 @RequestParam(value = "website") String website,
							 ModelMap model) {
		model.put("current", "register");
		if (null != this.companyDao.retriveByName(name)) {
			model.put("error", "Name already exist");
		} else {
			CompanyDo company = new CompanyDo();
			company.setName(name);
			company.setPwd(pwd);
			company.setAddress(address);
			company.setPhone(phone);
			company.setWebsite(website);
			this.companyDao.create(company);
			model.put("msg", "Register successfully, please login");
		}
		return "page/company/login";
	}
	
	@RequestMapping(value = "/company/list")
	@ResponseBody
	public List<CompanyDo> getList() {
		return this.companyDao.retrive();
	}
	
	@RequestMapping(value = "/company/id/{id}", method = RequestMethod.GET)
	@ResponseBody
	public CompanyDo getOne(@PathVariable("id") Integer id) {
		return this.companyDao.retriveById(id);
	}
	
	@RequestMapping(value = "/company", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo add(@RequestParam(value = "name") String name,
						@RequestParam(value = "pwd") String pwd,
						@RequestParam(value = "address") String address,
						@RequestParam(value = "phone") String phone,
						@RequestParam(value = "website") String website) {
		CompanyDo company = new CompanyDo();
		company.setName(name);
		company.setPwd(pwd);
		company.setAddress(address);
		company.setPhone(phone);
		company.setWebsite(website);
		this.companyDao.create(company);
		return new ResultVo(true, null, company);
	}
	
	@RequestMapping(value = "/company/id/{id}", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo modify(@PathVariable(value = "id") Integer id,
							@RequestParam(value = "name") String name,
							@RequestParam(value = "pwd") String pwd,
							@RequestParam(value = "address") String address,
							@RequestParam(value = "phone") String phone,
							@RequestParam(value = "website") String website) {
		CompanyDo company = new CompanyDo();
		company.setId(id);
		company.setName(name);
		company.setPwd(pwd);
		company.setAddress(address);
		company.setPhone(phone);
		company.setWebsite(website);
		this.companyDao.update(company);
		return new ResultVo(true, null, company);
	}
	
	@RequestMapping(value = "/company/id/{id}", method = RequestMethod.DELETE) 
	@ResponseBody
	public ResultVo remove(@PathVariable(value = "id") Integer id) {
		this.companyDao.delete(id);
		return new ResultVo();
	}
}
